package com.juyu.miliao.api.dao.redis;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.util.RedisKeys;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

@Repository
public class ChatSpendRedisDao {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public void setRoomId(String roomId){
        stringRedisTemplate.opsForValue().set(RedisKeys.SET_ROOMID.getValue(roomId),
                Instant.now().getEpochSecond()+"",
                1,TimeUnit.DAYS);
    }

    public boolean isMemberRoomId(String roomId){
        return stringRedisTemplate.hasKey(RedisKeys.SET_ROOMID.getValue(roomId));
    }
    public void chatSpendCounter(String idKey){
        stringRedisTemplate.opsForValue().increment(RedisKeys.STRING_CHATSPEND_COUNTER.getValue(idKey), 1);
    }
    public int getChatSpendNum(String idKey){
        String temp = stringRedisTemplate.opsForValue().get(RedisKeys.STRING_CHATSPEND_COUNTER.getValue(idKey));
        if(temp ==null || temp.equals("null") || temp.equals(""))
            temp = "0";
        return Integer.parseInt(temp);
    }
    public void deleteChatSpendCounter(String idKey){
        stringRedisTemplate.delete(RedisKeys.STRING_CHATSPEND_COUNTER.getValue(idKey));
    }

    public void setRoomIdCallNum(String roomId){
        String temp = getRoomIdCallNum(roomId);
        if(temp == null || temp.equals("") || temp.equals("null") || temp.equals("0")){
            stringRedisTemplate.opsForValue().set(RedisKeys.STRING_ROOMID_CALLNUM.getValue(roomId), "1", 1,TimeUnit.DAYS);
        }else{
            stringRedisTemplate.opsForValue().increment(RedisKeys.STRING_ROOMID_CALLNUM.getValue(roomId),1);
        }
    }

    public String getRoomIdCallNum(String roomId){
        return stringRedisTemplate.opsForValue().get(RedisKeys.STRING_ROOMID_CALLNUM.getValue(roomId));
    }
//1.5.4使用

    /**
     * 增加一条计费
     * @param roomId
     */
    public void addChatSpendRoomId(String roomId){
        stringRedisTemplate.opsForList().leftPush("chatSpendRoomId:"+roomId,Instant.now().getEpochSecond()+"");
    }

    /**
     * 查询计费次数
     * @param roomId
     * @return
     */
    public Long getListSizeByChatSpendRoomId(String roomId){
        return stringRedisTemplate.opsForList().size("chatSpendRoomId:"+roomId);
    }

    /**
     * 查询最后一条计费 时间
     * @param roomId
     * @return
     */
    public String getEndDataByChatSpendRoomId(String roomId){
        Long size = getListSizeByChatSpendRoomId(roomId);
        return stringRedisTemplate.opsForList().index("chatSpendRoomId:"+roomId,size-1);
    }

    /**
     * 添加json账单
     * @param detailJson
     */
    public void addCallDetailRecord(String roomId,JSONObject detailJson) {
        stringRedisTemplate.opsForValue().set("callDetailRecord:"+roomId,detailJson.toString(),1,TimeUnit.HOURS);
    }

    public String getCallDetailRecord(String roomId){
        return stringRedisTemplate.opsForValue().get("callDetailRecord:"+roomId);
    }
}
